<?php
namespace Admin\Controller;
use Think\Controller;
class PageController extends Controller {
    /*
     * 分页类
     * @param  string   $table        表名
     * @param  string   $field         字段名
     * @param  string   $where     条件
     * @param  string   $order       排序字段
     * @param  string   $sum         显示数量
     * @param  string   $join          开启join查询   例如 1开启 0 不开启
     */
    public function page($table,$field,$where,$order,$sum,$join) {
        $datas = M($table); // 实例化User对象
        if (empty($field)) {
            $field = '*';
        }

        $g = (I('get.p'));
        if(empty($g)) {
            $g = 1;
        }
		if ($join === 0 && $join != 1) {
            $list = $datas->field($field)->where($where)->order($order)->page($g.','.$sum)->select();
            $count      = $datas->where($where)->count();// 查询满足要求的总记录数
        } else if($join === 1) {
            $list = $datas->field($field)->join($where)->order($order)->page($g.','.$sum)->select();
            $count      = $datas->join($where)->count();// 查询满足要求的总记录数
        }


		$Page       = new \Think\Page($count,$sum);// 实例化分页类 传入总记录数和每页显示的记录数

        $shu = $count/$sum;
        $total = $shu<1?1:$shu;

        $info['records'] = $count;
        $info['total'] = $total;
//        $Page -> setConfig('header','共%TOTAL_ROW%条');
        $Page -> setConfig('first','首页');
        $Page -> setConfig('last','共%TOTAL_PAGE%页');
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $Page -> setConfig('link','indexpagenumb');//pagenumb 会替换成页码
        $Page -> setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show       = $Page->show();// 分页显示输出
        $data = array($list,$show,$info);
        return $data;
    }

}